// pages/phoneCode/phoneCode.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: null,
    deleIcon: false,
    code_top: false,
    phone_top: true,
    mobile: null,
    codeTime: 60,
    code: null,
  },
  codeSubmit(){
    console.log('重新发送验证码')
    this.setData({
      codeTime:60
    })
    this.codeTimeDown()
  },
  codeTimeDown() {
    var that = this;
   that.data.Interval = setInterval(item => {
      let codeTime = that.data.codeTime -= 1
      if(codeTime === 0){
        clearInterval(that.data.Interval)
      }
      that.setData({
        codeTime: codeTime
      })
      // console.log(codeTime)
    }, 1000)
  }, 
  submit() {
    var telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    var inputStr = this.data.phone;
    if (!(telStr.test(inputStr))) {
      console.log('手机号码输入不规范');
      wx.showToast({
        title: '手机号不正确',
        icon: 'error',
        duration: 1500
      })
    } else {
      console.log('手机号码输入规范1111');
      var mobile = this.data.phone;
      // 发送验证码 拿到一个uuid 提交时验证
      app.ports.phoneCode({data: {phone: this.data.phone}}).then((res) => {
        mobile = mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
        this.setData({
          code_top: true,
          phone_top: false,
          mobile: mobile,
          uuid: res.data.uuid,
        })
        this.codeTimeDown()
      }).catch(() => {
        console.error('获取验证码失败');
      });

      // 不发送测试作用
      // mobile = mobile.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
      // this.setData({
      //   code_top: true,
      //   phone_top: false,
      //   mobile: mobile
      // })
    }
  },
  submit1(){
    let that = this;
    console.log('提交了',this.data);
    let data = {
      phone: this.data.phone,
      uuid: this.data.uuid
    }
    var telStr = /^\d{6}$/;
    var inputStr = this.data.code;
    if (!(telStr.test(inputStr))) {
      console.log('验证码输入不规范', this.data);
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'error',
        duration: 1500
      })
      
    }else{
      data.code = this.data.code;
      app.ports.codeVali({data: data}).then((res) => {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];//上一个页面
        let userInfo = prevPage.data.userInfo;
        userInfo.phone = that.data.phone;
        prevPage.setData({
          userInfo: userInfo
        })
        wx.navigateBack({
          delta: 1,
        })
      }).catch(() => {
        console.error('手机号修改失败');
      });
    }
  },
  delePhone() {
    this.setData({
      phone: ''
    })
  },
  phoneInput(e) {
    let phone = e.detail.value
    let deleIcon = (phone.length >= 1 ? true : false)
    this.setData({
      phone: phone,
      deleIcon: deleIcon
    })
  },
  codeInput(e){
    this.setData({
      code: e.detail
    })
  },
  showDele() {
    if (this.data.phone && this.data.phone.length >= 1) {
      this.setData({
        deleIcon: true
      })
    }
  },
  hideDele() {
    this.setData({
      deleIcon: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // app.ports.phoneCode({}).then((res) => {

    // }).catch(() => {
    //   console.error('获取验证码失败');
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.Interval)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.Interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})